package com.dehong.controller;

import com.dehong.model.ExtCproductC;
import com.dehong.model.FactoryC;
import com.dehong.service.ContractProductCService;
import com.dehong.service.ExtCproductCService;
import com.dehong.service.FactoryCService;
import com.dehong.vo.ContractPCVo;
import com.dehong.vo.ExContractPCVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
public class ExtCproductCController {
@Autowired
    private ExtCproductCService extCproductCService;
@Autowired
private FactoryCService factoryCService;
@Autowired
  private   ContractProductCService contractProductCService;
@RequestMapping("selectextpc")
public String  selectextpc(String contractProductId, Model model, HttpServletRequest request){

    List<FactoryC> selectfactory = factoryCService.selectfactory();
    List<ContractPCVo> contractPCVos = contractProductCService.selectCP();
    model.addAttribute("ctypeList",contractPCVos);
    model.addAttribute("factoryList",selectfactory);
    if (request.getSession().getAttribute("contractProductId")==null){
        request.getSession().setAttribute("contractProductId",contractProductId);
        List<ExContractPCVo> list = extCproductCService.selectextpc(contractProductId);
        model.addAttribute("dataList",list);
    }else {
        String contractProductId1 = (String) request.getSession().getAttribute("contractProductId");
        List<ExContractPCVo> list = extCproductCService.selectextpc(contractProductId1);
        model.addAttribute("dataList",list);
    }

    return "pages/basicinfo/ExtCproductc/jExtCproductCreate";
}
@RequestMapping("insertexpc")
    public  String insertexpc(HttpServletRequest request,ExtCproductC extCproductC){
   extCproductC.setExtCproductId(UUID.randomUUID().toString());
    int insert = extCproductCService.insertSelective(extCproductC);
    if (insert>0){
        request.getSession().setAttribute("contractProductId",extCproductC.getContractProductId());
        return "redirect:/selectextpc.action";
    }else {
        return "error";
    }
}
@RequestMapping("goupdatexpc")
    public  String goupdate(ExtCproductC extCproductC,Model model){
    ExContractPCVo extCproductC1 = (ExContractPCVo) extCproductCService.selectByPrimaryKey(extCproductC.getExtCproductId());
    List<FactoryC> selectfactory = factoryCService.selectfactory();
    List<ContractPCVo> contractPCVos = contractProductCService.selectCP();
    model.addAttribute("obj",extCproductC1);
    model.addAttribute("ctypeList",contractPCVos);
    model.addAttribute("factoryList",selectfactory);
    return "pages/basicinfo/ExtCproductc/jExtCproductUpdate";
}
@RequestMapping("updateexpc")
    public String updateexpc(ExtCproductC extCproductC,HttpServletRequest request){
    System.out.println("id"+extCproductC.getExtCproductId());
    int i = extCproductCService.updateByPrimaryKeySelective(extCproductC);
    if (i>0){
        request.getSession().setAttribute("contractProductId",extCproductC.getContractProductId());
        return "redirect:/selectextpc.action";
    }else {
        return "error";
    }
}
@RequestMapping("deleteext")
    public String deleteext(String extCproductId){
    int i = extCproductCService.deleteByPrimaryKey(extCproductId);
    if (i>0){
        return "redirect:/selectextpc.action";
    }else {
        return "error";
    }
}

}
